﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.DTO;
using DoAn.BUS;

namespace DoAn.BAN
{
    public partial class BanTrang5 : System.Web.UI.Page
    {
        SanDauGiaDTO san=new SanDauGiaDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadThongTinSanPham();
            LoadThongTinGiaCa();
            ThanhToanVaVanChuyen();
            labSoTienPhaiTra.Text += TienPhaiTra().ToString();
        }

        protected void bntBan_Click(object sender, EventArgs e)
        {
            ThemSanDauGia();
            ThemChiPhiVC();
            ThemChiTietTT();
            UploadFile();
            Response.Redirect("~/BAN/BanTrang1.aspx");
        }

        public void LoadThongTinSanPham()
        {
            san=(SanDauGiaDTO)HttpContext.Current.Session["SanPham"];
            DanhMucDTO dm=new DanhMucDTO();
            dm.MaDanhMuc=san.MaDanhMuc;
            labTenDanhMuc.Text = DanhMucBUS.layTenDanhMuc(dm);
            labTenSanPham.Text = san.TenSan;
            labMoTa.Text = san.MoTa;
            labMoTaChiTiet.Text = san.MoTaChiTiet;
        }

        public void LoadThongTinGiaCa()
        {
            labGiaKhoiDiem.Text += san.GiaKhoiDiem.ToString();
            if (san.MuaNgay)
            {
                labMuaNhanh.Text = "Cho Phép Mua Ngay";
                labGiaMuaNhanh.Text += san.GiaMuaNgay.ToString();
            }
            else
            {
                labMuaNhanh.Text = "Không Cho Phép Mua Ngay";
                labGiaMuaNhanh.Text += 0;
            }
            labThoiGianDauGia.Text = HttpContext.Current.Session["ThoiGianDauGia"].ToString();
        }

        public void ThanhToanVaVanChuyen()
        {
            List<HinhThucThanhToanDTO> liht = new List<HinhThucThanhToanDTO>();
            liht = (List<HinhThucThanhToanDTO>)HttpContext.Current.Session["HinhThucTT"];
            for(int i=0;i<liht.Count();i++)
            {
                labHinhThucThanhToan.Text += "\n"+(i+1).ToString() + ". " + HinhThucThanhToanBUS.layHTThanToan(liht[i]) + "\n";
            }
            VungMienDTO vm=new VungMienDTO();
            vm = (VungMienDTO)HttpContext.Current.Session["VungMien"];
            labVung.Text = VungMienBUS.layTenVung(vm);
            labDiaDiemVanChuyen.Text = san.DiaChiShip;
            List<ChiPhiVanChuyenDTO> livc = new List<ChiPhiVanChuyenDTO>();
            livc = (List<ChiPhiVanChuyenDTO>)HttpContext.Current.Session["PhiVanChuyen"];
            labGiaChuyenPhatNhanh.Text += livc[0].CuocPhi.ToString();
            labGiaChuyenPhatNhanh1.Text += livc[1].CuocPhi.ToString();
            labGhiChu.Text += livc[0].GhiChu;
            labGhiChu1.Text += livc[1].GhiChu;
            if (san.HoTroTraSP)
            {
                labTraHang.Text = "Cho Phép Trả Sản Phẩm";

            }
            else
            {
                labTraHang.Text = "Không Cho Phép Trả Sản Phẩm";
            }
        }

        public float TienPhaiTra()
        {
            float tien=0;
            if (san.MuaNgay == true)
            {
                tien =tien + 5000;
            }
            HttpFileCollection file;
            file = (HttpFileCollection)HttpContext.Current.Session["HinhAnh"];
            if (file.Count > 2)
            {
                for (int i = 2; i < file.Count; i++)
                {
                    tien = tien + 5000;
                }
            }
            return tien;
        }

        public void ThemSanDauGia()
        {
            SanDauGiaDTO addsan = new SanDauGiaDTO();
            addsan.DiaChiShip = san.DiaChiShip;
            addsan.GiaKhoiDiem = san.GiaKhoiDiem;
            addsan.GiaMuaNgay = san.GiaMuaNgay;
            addsan.GiaSanHienTai = san.GiaKhoiDiem;
            addsan.HinhAnh = san.HinhAnh;
            addsan.HinhThucHoanLaiTien = san.HinhThucHoanLaiTien;
            addsan.HinhThucKetThuc = true;
            addsan.HoTroTraSP = san.HoTroTraSP;
            addsan.KetThucSan = false;
            addsan.MaDanhMuc = san.MaDanhMuc;
            addsan.MoTa = san.MoTa;
            addsan.MoTaChiTiet = san.MoTaChiTiet;
            addsan.MuaNgay = san.MuaNgay;
            addsan.NgayDangSP = san.NgayDangSP;
            addsan.NgayKetThucDang = san.NgayKetThucDang;
            addsan.NgayKetThucSan = san.NgayKetThucDang;
            addsan.SoLanRaGia = 0;
            addsan.SoNguoiRaGia = 0;
            addsan.TenSan = san.TenSan;
            addsan.TenTaiKhoan = HttpContext.Current.Request.Cookies["TenTaiKhoan"].Value;
            addsan.ThoiGianTraSP = san.ThoiGianTraSP;
            SanDauGiaBUS.taoSanDauGia(addsan);
        }

        public void ThemChiTietTT()
        {
            List<HinhThucThanhToanDTO> liht = new List<HinhThucThanhToanDTO>();
            liht = (List<HinhThucThanhToanDTO>)HttpContext.Current.Session["HinhThucTT"];
             for(int i=0;i<liht.Count();i++)
             {
                 ChiTietThanhToanDTO ct = new ChiTietThanhToanDTO();
                 ct.MaSan = SanDauGiaBUS.maSanDauGia();
                 ct.MaHinhThucTT = liht[i].MaHinhThucTT;
                 ChiTietThanhToanBUS.themChiTietTT(ct);
             }
        }

        public void ThemChiPhiVC()
        {
            List<ChiPhiVanChuyenDTO> livc = new List<ChiPhiVanChuyenDTO>();
            livc = (List<ChiPhiVanChuyenDTO>)HttpContext.Current.Session["PhiVanChuyen"];
            for (int i = 0; i < livc.Count; i++)
            {
                livc[i].MaSan = SanDauGiaBUS.maSanDauGia();
                ChiPhiVanChuyenBUS.themChiPhiVanChuyen(livc[i]);
            }
        }

        public void UploadFile()
        {
            HttpFileCollection hfc = (HttpFileCollection)HttpContext.Current.Session["HinhAnh"];
            for (int i = 0; i < hfc.Count; i++)
            {
                try
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("HinhAnh") + "\\" + System.IO.Path.GetFileName(hpf.FileName));
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
               
               
                //for (int i = 1; i < hfc.Count; i++)
                //{
                //    HttpPostedFile hpf = hfc[i];
                //    if (hpf.ContentLength > 0)
                //    {
                //        //hpf.SaveAs(Server.MapPath("Hinh") + "\\" + System.IO.Path.GetFileName(hpf.FileName));
                //        Response.Write("<script>alert('File: " + hpf.FileName + "  Size: " +
                //         hpf.ContentLength + "  Type: " + hpf.ContentType + " Uploaded Successfully');</script>");
                //    }
                //}
            
        }

    }
}